package com.example.gw_back_end.controller;

import com.example.gw_back_end.domain.PageParam;
import com.example.gw_back_end.domain.Result;
import com.example.gw_back_end.domain.entities.Category;
import com.example.gw_back_end.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {

    private final CategoryService categoryService;
    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    // 获取游戏类别列表
    @GetMapping("get_category_list")
    public Result getCategoryList(PageParam pageParam) {
        return Result.success(categoryService.getCategoryList(pageParam));
    }


    // 新增游戏类别
    @PostMapping("add_category")
    public Result addCategory(Category category) {
        if (categoryService.addCategory(category)) {
            return Result.success();
        } else {
            return Result.error("新增类别失败,请稍后重试！");
        }
    }

    // 删除游戏类别
    @DeleteMapping("delete_category")
    public Result deleteCategory(@RequestBody List<Integer> ids) {
        if (categoryService.deleteCategory(ids)) {
            return Result.success();
        } else {
            return Result.error("删除类别失败,请稍后重试！");
        }
    }

    // 获取游戏类别
    @GetMapping("get_category")
    public Result getCategory(Integer id) {
        return Result.success(categoryService.getCategoryById(id));
    }

    // 更新游戏类别
    @PutMapping("update_category")
    public Result changeCategory(Category category) {
        if (categoryService.updateCategory(category)) {
            return Result.success();
        } else {
            return Result.error("更新类别失败，请稍后重试！");
        }
    }
}
